<?php

namespace brainmaster;

use Doctrine\ORM\Tools\Setup;
use \Doctrine\ORM\EntityManager;

class SessionFactory {
	
	public static function getSession($classDirectory,$entityDirectory,$properties,$isDevMode) {
		//Setup::registerAutoloadDirectory($classDirectory);
		$config = Setup::createAnnotationMetadataConfiguration(array($entityDirectory), $isDevMode);
		$data = fopen($properties,"r");
		
		$props = array();
		
		while (!feof($data)) {
			$lineString = fgets($data);
			$keyPair = explode("=",trim($lineString));
			$props[$keyPair[0]] = $keyPair[1];
		}
		
		fclose($data);
		
		$conn = array(
		    'driver' => $props["mobi.db_driver"],
		    'dbname' => $props["mobi.db_name"],
		    'user' => $props["mobi.db_username"],
		    'password' => $props["mobi.db_password"],
		    'host' => $props["mobi.db_host"],
		);
		
		return EntityManager::create($conn, $config);
	}
}